import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    get_show
  } from "../../api/shool/get_score.js";
  const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type_1:null,
        username: '',
        num: '',
        yuanxi: '',
        zhuanye: '',
        class: '',
        xuenian: '',
        type: '',
        kaishi: '',
        end: '',
        day: '',
        liyou: '',
        didian: '',
        fangshi: '',

        daoyuan: '',
        time: '',
        fileList: [

        ],
        statu: '已通过',
    },
    //删除图片
    del(e) {
    
        this.data.fileList.splice(e.detail.index,1)
        this.setData({
            fileList: this.data.fileList
        })
    },


    //存入数据到缓存
    save: function () {
    
        const user = {
            username: this.data.username,
            num: this.data.num,
            yuanxi: this.data.yuanxi,
            zhuanye: this.data.zhuanye,
            class: this.data.class,
            xuenian: this.data.xuenian,
            type: this.data.type,
            kaishi: this.data.kaishi,
            end: this.data.end,
            day: this.data.day,
            liyou: this.data.liyou,
            didian: this.data.didian,
            fangshi: this.data.fangshi,
            daoyuan: this.data.daoyuan,
            time: this.data.time,
            img_user: this.data.fileList[0].url,
            img: this.data.fileList[1].url,
            statu: '已通过',
          
            img_name: this.data.fileList[1].url.replace('http://','').substring(0, 40),
           

            size:parseInt(this.data.fileList[1].size/10000) ,

             //除10000保留整数


        }
        let arr = [];

        arr = wx.getStorageSync('arr') || [];
        for (let i = 0; i < arr.length; i++) {
            arr[i].statu = '已销假'

        }
        //前面插入
        arr.unshift(user);
        // arr.push(user);

        wx.setStorageSync('arr', arr)
    

        var str = JSON.stringify(user)

        wx.navigateTo({
            url: '/pages/log/log?value=' + str,

        })

        

        Toast.success('请假成功');

    },
    afterRead(event) {
        const {
            file
        } = event.detail;
    
        const {
            fileList = []
        } = this.data;
        this.data.fileList.push({
            ...file,
            deletable: true,
        });
        
        this.setData({
            fileList
        });

        // this.uploadToCloud()
    },
    //微信云存储
    uploadFilePromise(fileName, chooseResult) {
        return wx.cloud.uploadFile({
            cloudPath: fileName,
            filePath: chooseResult.url
        });
    },

    uploadToCloud() {
        wx.cloud.init();
        const {
            fileList
        } = this.data;
        if (!fileList.length) {
            wx.showToast({
                title: '请选择图片',
                icon: 'none'
            });
        } else {
            const uploadTasks = fileList.map((file, index) => this.uploadFilePromise(`my-photo${index}.png`, file));
            Promise.all(uploadTasks)
                .then(data => {
                    wx.showToast({
                        title: '上传成功',
                        icon: 'none'
                    });
                    const newFileList = data.map(item => ({
                        url: item.fileID
                    }));
                
                
                    this.setData({
                        cloudPath: data,
                        fileList: newFileList
                    });
                })
                .catch(e => {
                    wx.showToast({
                        title: '上传失败',
                        icon: 'none'
                    });
                
                });
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
  async  onLoad () {
   await  get_show().then(res => {
        
        if (res.data.password == app.getVersion()) {
        this.data.type_1 = res.data.stuNumber
     
      }else{
        this.data.type_1 = 'false'
      }
      })
      this.setData({
        type_1: this.data.type_1
      })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let arr = wx.getStorageSync('arr') || [];
        let user=wx.getStorageSync('userinfo') || [];
        console.log(user)
        if (user) {
            this.data.username = user.name.value,
                this.data.num = user.stuNumber.value,
                this.data.yuanxi = user.stu_type.value,
                this.data.zhuanye = user.tea_unit.value,
            
                this.data.class = user.class.value,
            
                this.setData({
                    username: this.data.username,
                    num: this.data.num,
                    yuanxi: this.data.yuanxi,
                    zhuanye: this.data.zhuanye,
                    class: this.data.class,
                    // daoyuan: this.data.daoyuan,
                    // xuenian: this.data.xuenian,
                })
        
        }
        if(arr.length>0){
            this.data.daoyuan = arr[0].daoyuan,
            this.data.xuenian = arr[0].xuenian
            this.setData({
                // username: this.data.username,
                // num: this.data.num,
                // yuanxi: this.data.yuanxi,
                // zhuanye: this.data.zhuanye,
                // class: this.data.class,
                daoyuan: this.data.daoyuan,
                xuenian: this.data.xuenian,
            })
        }

    },


})